package io.renren.wechat.param;

import cn.hutool.json.JSONUtil;
import io.renren.wechat.support.SignUtils;
import io.renren.wechat.support.WechatConstants;
import lombok.Data;

import javax.validation.constraints.NotBlank;

/**
 * @author dmcp@qq.com
 * @since 2022/3/31 23:17
 */
@Data
public class SyncCorpParam {

    private String corpName;

    @NotBlank
    private String corpId;

    @NotBlank
    private String agentId;

    @NotBlank
    private String sign;

    @NotBlank
    private String timestamp;

    public static SyncCorpParam convertFor(String corpId, String corpName, String agentId) {
        SyncCorpParam syncCorpParam = new SyncCorpParam();
        syncCorpParam.setAgentId(agentId);
        syncCorpParam.setCorpId(corpId);
        syncCorpParam.setCorpName(corpName);
        syncCorpParam.setTimestamp(String.valueOf(System.currentTimeMillis()));

        String body = JSONUtil.toJsonStr(syncCorpParam);
        String sign = SignUtils.sign(WechatConstants.ZIJI_INNER_KEY, null, syncCorpParam.getTimestamp(), null, null, body);
        syncCorpParam.setSign(sign);

        return syncCorpParam;
    }
}
